package com.adgox.tiantianbiting.mine.tingCoinRecord;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.adapter.TingCoinRecordAdapter;
import com.adgox.tiantianbiting.bean.PageBean;
import com.adgox.tiantianbiting.bean.RechargeInfoBean;
import com.adgox.tiantianbiting.bean.TradeRecordBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.mine.TingCoinWithdraw.TingCoinWithdrawActivity;
import com.adgox.tiantianbiting.mine.mobile.BindMobileActivity;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.google.gson.Gson;
import com.rae.swift.session.SessionManager;

import java.util.HashMap;
import java.util.HashSet;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TingCoinRecordActivity extends AppCompatActivity implements TingCoinRecordContract.View, View.OnClickListener {

    @BindView(R.id.tv_tips_1)
    TextView tvTips1;
    @BindView(R.id.tv_tips_2)
    TextView tvTips2;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;
    private TingCoinRecordContract.Presenter mPresenter;
    private int currentPage = 1;
    private int maxPage = 20;
    private EditText etName;
    private EditText etMobile;
    private EditText etVert;
    private Dialog dialog;
    private TextView tvSendVert;
    private int vertifyCD;
    private int userId;
    private String mobile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ting_coin_record);
        ButterKnife.bind(this);
        new TingCoinRecordPresenterImpl(this);
        userId = ((UserInfo) SessionManager.getDefault().getUser()).getId();
        mPresenter.start();
        mPresenter.getRecordData(currentPage, maxPage);
    }

    @Override
    public void setPresenter(TingCoinRecordContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void onSetTips1(RechargeInfoBean bean) {
        tvTips1.setText("提示: " + bean.getTingCoin() + "听币 = " + bean.getCnyCoin() + "元,");

        UserInfo userInfo = SessionManager.getDefault().getUser();
        tvTips2.setText(" 可提现" + userInfo.getCommission() + "元");

        tvAmount.setText("" + userInfo.getCommission());
    }

    @Override
    public void onsetTips1Failed(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSetRvData(PageBean<TradeRecordBean> beans) {
        if (beans == null || beans.getData() == null || beans.getData().size() == 0) {
            llEmpty.setVisibility(View.VISIBLE);
            rv.setVisibility(View.GONE);
            return;
        }
        llEmpty.setVisibility(View.GONE);
        rv.setVisibility(View.VISIBLE);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);
        TingCoinRecordAdapter tingCoinRecordAdapter = new TingCoinRecordAdapter(beans.getData());
        rv.setAdapter(tingCoinRecordAdapter);
    }

    @Override
    public void onSetRvDataFailed(String msg) {
        llEmpty.setVisibility(View.VISIBLE);
        rv.setVisibility(View.GONE);
    }

    @Override
    public void onGetVertSuccess() {
        Toast.makeText(this, "获取验证码成功", Toast.LENGTH_SHORT).show();
        tvSendVert.setClickable(false);
        vertifyCD = 60;
        handler.post(runnable);
    }

    @Override
    public void onGetVertFailed(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCheckVertSuccess() {
        startActivity(new Intent(this, TingCoinWithdrawActivity.class));
        dialog.dismiss();
    }

    @Override
    public void onCheckVertFailed(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            vertifyCD -- ;
            tvSendVert.setText(vertifyCD+"s");
            if (vertifyCD == 0) {
                tvSendVert.setText("点击获取");
                tvSendVert.setClickable(true);
                handler.removeCallbacks(runnable);
            } else {
                handler.postDelayed(runnable, 1000);
            }
        }
    };

    @OnClick(R.id.img_finish)
    public void onFinishClicked() {
        finish();
    }

    @OnClick(R.id.tv_withdraw)
    public void onWithdrawClicked() {
        startActivity(new Intent(this,TingCoinWithdrawActivity.class));

    }

    private void popCheckDialog() {
        dialog = new Dialog(this);
        Window window = dialog.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setContentView(R.layout.layout_withdraw_check_dialog);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.BOTTOM);
        window.setDimAmount(0.8f);
        ImageView ivCancel = dialog.findViewById(R.id.iv_cancel);
        etName = dialog.findViewById(R.id.et_name);
        etMobile = dialog.findViewById(R.id.et_phone);
        etVert = dialog.findViewById(R.id.et_vert);
        tvSendVert = dialog.findViewById(R.id.tv_send_vert);
        TextView tvCommit = dialog.findViewById(R.id.tv_commit);

        ivCancel.setOnClickListener(this);
        tvSendVert.setOnClickListener(this);
        tvCommit.setOnClickListener(this);
        dialog.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_cancel:
                dialog.dismiss();
                break;
            case R.id.tv_send_vert:
                //点击发送验证码
                sendVert();
                break;
            case R.id.tv_commit:
                //验证并提交
                checkVertAndCommit();
                break;
        }
    }

    private void checkVertAndCommit() {
        if (mobile == null||!mobile.equals(etMobile.getText().toString().trim())){
            Toast.makeText(this, "手机号为空或前后不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        if (etVert.getText().toString().trim().isEmpty()){
            Toast.makeText(this, "验证码不可为空", Toast.LENGTH_SHORT).show();
            return;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile",etMobile.getText().toString().trim());
        map.put("name",etName.getText().toString().trim());
        map.put("userId",userId);
        map.put("verCode",etVert.getText().toString().trim());
        Gson gson = new Gson();
        String s = gson.toJson(map);
        DialogUtils.showLoadingDialog(this);
        mPresenter.checkVert(s);
    }

    private void sendVert() {
        if (etName.getText().toString().trim().isEmpty()){
            Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (etMobile.getText().toString().trim().isEmpty()){
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!etMobile.getText().toString().trim().matches("[1]\\d{10}")){
            Toast.makeText(this, "手机号格式不对", Toast.LENGTH_SHORT).show();
            return;
        }
        mobile = etMobile.getText().toString().trim();
        DialogUtils.showLoadingDialog(this);
        mPresenter.sendVert(mobile,userId);
    }
}
